
$(document).ready(function() {

  $("#physician_search").click(function(){
    openDialog("physiciansearch",0,400,220,"Invite Physician");
    return false;
  });

  $('#edit-physycian-search-datesend').datepicker({
    dateFormat:'mm-dd-yy'
  });

  $("#edit-addphysician").click(function(){
    openDialog("addphysician",0,350,180,"Add Physician");
    return false;
  });


});



function applyAction(pageno, action){
  //prepare the field values for filtering
  var search_fname       = $.trim($("#edit-physycian-search-fname").val());
  var search_lname       = $.trim($("#edit-physycian-search-lname").val());
  var search_datesend    = $.trim($("#edit-physycian-search-datesend").val());
  var search_speciality  = $.trim($("#edit-physycian-search-speciality").val());
  var search_status      = $.trim($("#edit-physycian-search-status").val());

  var sort = $('#hid_sort').val();
    
  //check for search criteria and return false if no value
  if (search_fname == "" && search_lname == "" && search_datesend == "" && search_speciality == "" && search_status == "") {
    openAlertDialog(300, 125, 'Information', 'Please enter the search criteria');
    return;
  }

  $('#listingResultset').html('<div style="padding-top:150px;padding-bottom:150px;text-align:center;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading..." /></div>');
  $("#pagingdivtop").hide();
  $("#pagingdivbottom").hide();

  $.ajax({
    type : "POST",
    url : baseurl+"physician_updator/actions/filter",
    data : "search_fname="+search_fname+"&search_lname="+search_lname+"&search_datesend="+search_datesend+"&search_speciality="+search_speciality+"&search_status="+search_status+(pageno>1?'&page_no='+pageno:'')+(sort!=""?'&sorting='+sort:''),
    cache : false,
    success : function (msg) {
      $("#listingResultset").html(msg);
			 
      var limit = $('#hid_limit').val();
      var page = parseInt(pageno,10);
      var counter = $("#hid_totalRec").val();
	
      if(counter>0){
        var str="";
        var to = page*limit;
        str += '<span>Showing <b>'+(counter>0?(page-1)*limit+1:0)+'</b> to <b>'+(to>counter?counter:to)+'</b> of <b>'+counter+'</b></span>';
        if (page > 1) {
          str += '<a href="javascript:void(0)" onclick = "applyAction('+(page-1)+',\''+action+'\')" class="red"><< Previous</a>';
        }

        if (to < counter) {
          str += '<span class="red"> |</span><a href="javascript:void(0)" onclick = "applyAction('+(page+1)+',\''+action+'\')" class="red"> Next >></a>';
        }

        $("#pagingdivtop").html(str);
        $("#pagingdivbottom").html(str);
        $("#pagingdivtop").show();
        $("#pagingdivbottom").show();
      }
		
    }
  });

}


 




function physician_view_dialog(id){
  $.ajax({
		type : "GET",
		url : baseurl+'physician_updator/viewdetail/'+id,
		cache : false,
		success : function (res) {
		  $('#dialog').html(res);
		},
		error : function (res) {
		  handleRequestError(res);
		}
  });
}


function edit_physician(id){
 
  //var id         = $("#edit-poll-id").val();
  //var poll_title = $("#poll_title_activate").html();

  reopen_dialog(500,230,'Edit Physicians');
  $.ajax({
		type : "GET",
		url : baseurl+'physician_updator/edit/'+id,
		cache : false,
		success : function (res) {
		  $('#dialog').html(res);
		  $('#edit-physician-date-send').datepicker({
			dateFormat:'mm-dd-yy'
		  });
		},
		error : function (res) {
		  handleRequestError(res);
		}
  });
}


function activationchange_physicians(id,status){
  var pageno                    =  $("#hid_pageno").val();
  var physician_fname           =  $('#physician_fname').html();
  var physician_lname           =  $('#physician_lname').html();
  var physician_datesend        =  $('#physician_datesend').html();
  var physician_speciality      =  $('#physician_speciality').html();
  var physician_statuscode      =  status;


  $('#dialog').html('<div style="padding-top:90px; padding-left:210px;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading... Please Wait !!!" title="Loading... Please Wait !!!" border="0"></div>');
   
  $.ajax({
    type: "POST",
    url: baseurl + 'physician_updator/update/'+id,
    data: '&physician_fname='+ physician_fname + '&physician_lname=' + physician_lname + '&physician_datesend=' + physician_datesend + '&physician_speciality=' + physician_speciality + '&physician_statuscode=' + physician_statuscode,
    success: function(message){
      applyAction(pageno,$('#edit-apply').attr('actionitem'));
      reopen_dialog(500,260,"View Physician");
      viewphysiciandialog(id);
      return false;			 
    },
    error : function (res) {
      handleRequestError(res);
    }
  });

  return false;
}



function viewphysiciandialog(id){

  $('#dialog').html('<div style="padding-top:90px; padding-left:210px;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading... Please Wait !!!" title="Loading... Please Wait !!!" border="0"></div>');

  $.ajax({
    type : "GET",
    url : baseurl+'physician_updator/viewdetail/'+id,
    cache : false,
    success : function (res) {
      $('#dialog').html(res);
    },
    error : function (res) {
      handleRequestError(res);
    }
  });
}


function save_physician(id){
   var pageno               = $("#hid_pageno").val();
   physician_fname          = $('#edit-physician-fname').val();
   physician_lname          = $('#edit-physician-lname').val();
   physician_datesend       = $('#edit-physician-date-send').val();
   physician_speciality     = $('#edit-physician-speciality').val();
   physician_statuscode     = $('#edit-physician-status').val();
  
   $('#dialog').html('<div style="padding-top:90px; padding-left:210px;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading... Please Wait !!!" title="Loading... Please Wait !!!" border="0"></div>');
    
   $.ajax({
      type: "POST",
      url: baseurl + 'physician_updator/update/'+id,
      data: '&physician_fname='+ encodeURIComponent(physician_fname) + '&physician_lname=' + encodeURIComponent(physician_lname) + '&physician_datesend=' + physician_datesend + '&physician_speciality=' + physician_speciality + '&physician_statuscode=' + physician_statuscode,
	  success: function(message){
      eval("var json="+message);
        if(json.Message=="Success"){
			  reopen_dialog(300,225,"Physician is Successfully Updated");
			  applyAction(pageno,$('#edit-apply').attr('actionitem'));
			  openAlertDialog(300, 125, 'Updated', 'Physician is Successfully Updated');
			  return false;
        }
        else{
          if(json.Error=="Physician name already exist"){
					$.ajax({
					  type : "GET",
					  url : baseurl+'physician_updator/viewdetail/'+id,
					  cache : false,
					  success : function (res) {
						$('#dialog').html(res);
						createDatePicker('edit-physician-date-send',0);
						styleStatusMessages("Physician name already exist", "statusMessage", "failure");
					  },
					  error : function (res) {
						handleRequestError(res);
					  }
					});
					resize_dialog(600,350+30);
          }
          else{
					$.ajax({
					  type : "GET",
					  url : baseurl+'physician_updator/viewdetail/'+id,
					  cache : false,
					  success : function (res) {
						$('#dialog').html(res);
						createDatePicker('edit-physician-date-send',0);
						styleStatusMessages("Failure to save Physician", "statusMessage", "failure");
					  },
					  error : function (res) {
						handleRequestError(res);
					  }
					});
					resize_dialog(600,350+30);
          }
        }
      },
      error : function (res) {
        handleRequestError(res);
      }		  

	  
    });
return false;
}


function confiure_email(){
  openDialog("phupdatormail",0,780,410,"Manage Auto Reply");
}

function phmanageautoreply(id){
		$.ajax({
		  type : "GET",
		  url : baseurl+'physician_updator/configure_email',
		  cache : false,
		  success : function (res) {
			$('#dialog').html(res);
			fckeditor('edit-invite-message');
		  },
		  error : function (res) {
			handleRequestError(res);
		  }
		});
}


function save_email_invitation(){	    
	    
		ph_firstname = $('#edit-invite-fname').val();
		ph_lastname  = $('#edit-invite-femail').val();
		ph_recipient = $('#edit-invite-recipient').val();
		ph_subject   = $('#edit-invite-subject').val();

		reopen_dialog(300,225,"Physician is Successfully Updated");

		$.ajax({
		  type : "POST",
		  url : baseurl+'physician_updator/configure_email/save',
		  data: '&firstname='+ph_firstname+'&lastname='+ph_lastname+'&recipient='+ph_recipient+'&subject='+ph_subject,
		  cache : false,
		  success : function (msg) {
			  if(msg == "Success"){
			     openAlertDialog(300,200,"Updated", "Email is configured and updated successfully");
			  }
			  else{
				 openAlertDialog(300,200,"Error", "Error in updating the email message");
			  }
		  },
		  error : function (res) {
			handleRequestError(res);
		  }
		});

}


function invite_physician(){
	  /*
		$("input:radio[@name='radioList']").each(function(){					
		    id = $("input:radio[@name='radioList']:checked").val();			
		});
		*/
		
         if($('input[name=radioList]').is(":checked") == false){
          alert('You should have to select record');
		  return false;
		 }

        id = $('input:radio[name=radioList]:checked').val();

		reopen_dialog(600,300,"Invite Physician");
		$.ajax({
		  type : "GET",
		  url : baseurl+'physician_updator/configure_email/invite/'+id,
		  cache : false,
		  success : function (res) {
			 $('#dialog').html(res);
		  },
		  error : function (res) {
			handleRequestError(res);
		  }
		});
}



function sendinvitation(){
		
		ph_fname          = $("#ph_fname").val();
        ph_lname          = $('#ph_lname').val();
		ph_npid           = $('#ph_npid').val();
		ph_emailaddress   = $('#ph_emailaddress').val();
		ph_speciality     = $('#ph_speciality').val();
        
	    reopen_dialog(300,225,"Email Sending Status");
              
		$.ajax({
		  type : "POST",
		  url : baseurl+'physician_updator/configure_email/sendemail/'+id,
		  data: '&physician_firstname='+ encodeURIComponent(ph_fname) + '&physician_lastname=' + encodeURIComponent(ph_lname)+ '&physician_email=' + encodeURIComponent(ph_emailaddress),
		  cache : false,
		  success : function (msg) {
			  if(msg == 'Success'){
			     openAlertDialog(300,200,"Email Sent", "Email is sent successfully");
			  }
			  else{
                 openAlertDialog(300,200,"Error", "Error in sending email");
			  }
		  },
		  error : function (res) {
			handleRequestError(res);
		  }
		});
}


function addphysician_view_dialog(){

		$.ajax({
		  type : "GET",
		  url : baseurl+'physician_updator/addphysician',
		  cache : false,
		  success : function (res) {
			 $('#dialog').html(res);
		  },
		  error : function (res) {
			handleRequestError(res);
		  }
		});
}

function listpendinginvitees(){
	    
		physician_fname          = $('#physician_firstname').val();
        physician_lname          = $('#physician_lastname').val();

		if(($.trim(physician_fname) == '') && ($.trim(physician_lname)=='')){		
             $('#dialog').scrollTop(0);
             styleStatusMessages("Please enter values at leat one field", "statusMessage", "failure");
			 resize_dialog(400, 240);
			 return false;
		}
        

		reopen_dialog(420,300,"Pending Invitees");
		$.ajax({
		  type : "POST",
		  url : baseurl+'physician_updator/pending_invitees',
		  data: '&physician_firstname='+ encodeURIComponent(physician_fname) + '&physician_lastname=' + encodeURIComponent(physician_lname),
			  
		  cache : false,
		  success : function (res) {
			 $('#dialog').html(res);
			//fckeditor('edit-invite-message');
		  },
		  error : function (res) {
			handleRequestError(res);
		  }
		});
}


function savephysician(){

		first_name          = $('#first_name').val();
        last_name           = $('#last_name').val();
		email_address       = $('#email_address').val();

		if(($.trim(first_name) == '') || ($.trim(last_name)=='') || ($.trim(email_address)=='') ){		
             $('#dialog').scrollTop(0);
             styleStatusMessages("Please enter values in all the field", "statusMessage", "failure");
			 resize_dialog(350,200);
			 return false;
		}
			

		reopen_dialog(300,200,"Add Physician");

		$.ajax({
		  type : "POST",
		  url : baseurl+'physician_updator/addphysician/add',
		  data: '&first_name='+ first_name + '&last_name=' + last_name+ '&email_address=' + email_address,
			  
		  cache : false,
		  success : function (msg) {
			  if(msg == 'Success'){
                 openAlertDialog(300,200,"Added", "Updated data successfully");
			  }
			  else{
                 openAlertDialog(300,200,"Error", "Error in updating data");
			  }

		  },
		  error : function (res) {
			handleRequestError(res);
		  }
		});
}


var ckeditor;
function fckeditor(id){
  if(!ckeditor){
    ckeditor = CKEDITOR.replace(id,{
      skin : 'v2',
      resize_enabled:false,
      enterMode: Number(2),
      removePlugins : 'elementspath,save,forms,font,format,smiley,maximize,showblocks,about,stylescombo,pagebreak,flash'
    });
  }else{
    if (CKEDITOR.instances[id]) {
      CKEDITOR.remove(CKEDITOR.instances[id]);
    }
    ckeditor=null;
    ckeditor = CKEDITOR.replace(id,{
      skin : 'v2',
      resize_enabled:false,
      enterMode: Number(2),
      removePlugins : 'elementspath,save,forms,font,format,smiley,maximize,showblocks,about,stylescombo,pagebreak,flash'
    });
  }
  $('.ui-dialog').css("filter","");
}






function reloadPhysicianupdatorList (limit, page, action, counter, url, location) {
  //page loading animation

  ph_pagination_loading(location);

  //Call the AJAX functionality and return the result
  var sort = $('#hid_sort').val();
 
  var pno = $('#hid_pop_pageno').val();
  
  physician_firstname = $('#physician_firstname').val();
  physician_lastname  = $('#physician_lastname').val();


  if(action=="previous"){
    page = parseInt(pno,10)-1;
  }else if(action=="next"){
    page = parseInt(pno,10)+1;
  }
	
  //remove the checkall checkbox if selected
  if ($("#checkAll")) {
    $("#checkAll").attr('checked', false);
  }
  

  $.ajax({
    type : "POST",
    url : url+"?limit="+limit+"&page="+page+"&mode=ajax"+"&physician_firstname="+physician_firstname+"&physician_lastname"+physician_lastname+(sort!=""?"&sorting="+sort:""),
    async : true,
    cache : false,
    success : function (msg) {
      if (location == "page") {
        $("#listingResultset").html(msg);
      }
      else if (location == "popup") {
        $("#listingResultsetPopup").html(msg);
      }
      //create pagination links through AJAX
      var str = "";
      var to = page*limit;
      //modified if the counter is less while filtering the records
      if (to > counter) {
        counter = $("#hid_totalRec").val();
      }
			
      str += '<span>Showing <b>'+(counter>0?(page-1)*limit+1:0)+'</b> to <b>'+(to>counter?counter:to)+'</b> of <b>'+counter+'</b></span>';
      if (page > 1) {
        str += '<a href="javascript:void(0)" onclick = "reloadPhysicianupdatorList('+limit+', '+0+', \'previous\', '+counter+', \''+url+'\', \''+location+'\')" class="red"><< Previous</a>';
      }
      if (to < counter) {
        str += '<span class="red"> |</span><a href="javascript:void(0)" onclick = "reloadPhysicianupdatorList('+limit+', '+0+', \'next\', '+counter+', \''+url+'\', \''+location+'\')" class="red">Next >></a>';
      }
			
      if (location == "popup") {
        if ($("#pagingdivtoppopup")) $("#pagingdivtoppopup").html(str);
        if ($("#pagingdivbottompopup")) $("#pagingdivbottompopup").html(str);
		$('#hid_pop_pageno').val(page);
      }
    },
    error : function (res) {
      handleRequestError(res);
    }
  });
}

//Pagination loading animation
function ph_pagination_loading (location){

  if (location == "popup") {
    $("#listingResultsetPopup").html('<div style="padding-top:150px;padding-bottom:150px;text-align:center;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading..." /></div>');
    $("#pagingdivtoppopup").html("");
    $("#pagingdivbottompopup").html("");
  }
}